package com.yao.amssm.config;


import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.ViewResolver;



@Configuration
@EnableWebMvc
@ComponentScan("com.yao.amssm.*.control")
public class SpringMvcConfig extends WebMvcConfigurerAdapter{
	
	@Bean
	public ViewResolver ViewResolver(){
		
		InternalResourceViewResolver reso = new InternalResourceViewResolver();
		reso.setPrefix("WEB-INF/views");
		reso.setSuffix(".jsp");
		reso.setExposeContextBeansAsAttributes(true);
		return reso;
	}

	// 配置静态资源的处理,对静态资源的请求转发到servlet容器中默认的servlet上(对静态资源的请求不做处理)
	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
    // 文件上传用的resolver
    @Bean
    public MultipartResolver multipartResolver() {
    	CommonsMultipartResolver cmr = new CommonsMultipartResolver();
    	cmr.setDefaultEncoding("utf-8");
    	cmr.setMaxInMemorySize(40960);
    	cmr.setMaxUploadSize(10485760000l);
        return cmr;
    }

	@Bean
    public ByteArrayHttpMessageConverter byteMsgConverter() {
        return new ByteArrayHttpMessageConverter();
    }
	
	@Bean
	public StringHttpMessageConverter stringHttpMessageConverter(){
		List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
		supportedMediaTypes.add(MediaType.APPLICATION_JSON);
		supportedMediaTypes.add(MediaType.TEXT_HTML);
		supportedMediaTypes.add(MediaType.TEXT_XML);
		StringHttpMessageConverter shmc = new StringHttpMessageConverter();
		shmc.setSupportedMediaTypes(supportedMediaTypes);
		return shmc;
	}


}
